package com.ttbj.controller.bill;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ttbj.api.bill.BillPieceService;
import com.ttbj.api.dto.Page;
import com.ttbj.api.dto.bill.BillCarTypeDto;
import com.ttbj.api.dto.bill.BillDistanceDto;
import com.ttbj.api.dto.bill.BillPieceDto;
import com.ttbj.api.dto.bill.BillSecondDto;
import com.ttbj.api.dto.bill.BillServeDto;
import com.ttbj.api.dto.bill.BillTimingDto;
import com.ttbj.api.dto.bill.BillVehicleDto;
import com.ttbj.api.exception.BusinessException;
import com.ttbj.constant.annotation.SystemControllerLog;
import com.ttbj.util.ViewDataUtil;

@Controller
public class BillPieceController {
	
	@Autowired
	private BillPieceService billPieceService;
	
	
	/**
	 * 按次，按件，按里程计费规则页面
	 * @return
	 */
	@RequestMapping(value="/bill/bill_num_piece.htm")
	public String bill_num_piece() {
		return "bill/bill_num_piece";
	}
	
	/**
	 * 服务类型，车辆类型权重页面
	 * @return
	 */
	@RequestMapping(value="/bill/bill_proportion.htm")
	public String bill_proportion() {
		return "bill/bill_proportion";
	}
	
	/**
	 * 时间配置页面
	 * @return
	 */
	@RequestMapping(value="/bill/bill_timing.htm")
	public String bill_timing() {
		return "bill/bill_timing";
	}
	
	/**
	 * 编辑时间配置页面
	 * @return
	 */
	@RequestMapping(value="/bill/edit_bill_timing.htm")
	public String edit_bill_timing() {
		return "bill/edit_bill_timing";
	}
	
	/**
	 * 车辆类型及车辆权重页面
	 * @return
	 */
	@RequestMapping(value="/bill/bill_car_type.htm")
	public String bill_car_type() {
		return "bill/bill_car_type";
	}
	
	/**
	 * 修改车辆类型及车辆权重页面
	 * @return
	 */
	@RequestMapping(value="/bill/edit_bill_car_type.htm")
	public String edit_bill_car_type() {
		return "bill/edit_bill_car_type";
	}
	
	/**
	 * 按次，按件，按里程计费规则信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/bill/getBillInfo.htm", method=RequestMethod.GET, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getBillNumPiece(HttpServletRequest request) {
		try {
			//获取按件计费信息
			BillPieceDto billPieceDto = this.billPieceService.findBillPieceInfo();
			//获取按次计费信息
			BillSecondDto billSecondDto = this.billPieceService.findBillSecondInfo();
			//获取按里程计费信息
			List<BillDistanceDto> billDistanceDtos = this.billPieceService.findBillDistanceInfo();
			Map<String, Object> map = new HashMap<>();
			map.put("billPiece", billPieceDto);
			map.put("billSecond", billSecondDto);
			map.put("billDistance", billDistanceDtos);
			return ViewDataUtil.buildSuccessJson("计费信息", map);
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 修改按次和按件以及按里程计费信息
	 * @param request
	 * @param billPieceDto 按件计费信息
	 * @param billSecondDto 按次计费信息
	 * @param billDistanceDto 按里程计费信息
	 * @param billType 计费类型：1按次，2按件，3按里程白天，4按里程夜间
	 * @return
	 */
	@RequestMapping(value="/bill/modifyBillInfo.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	@SystemControllerLog(description="修改计费信息配置")
	public String modifyBillInfo(BillPieceDto billPieceDto, BillSecondDto billSecondDto, 
					BillDistanceDto billDistanceDto, @RequestParam String billType) {
		try {
			String message = "";
			if (billType.equals("2")) {
				this.billPieceService.editBillPieceInfo(billPieceDto);
				message = "修改按件计费成功";
			}else if (billType.equals("1")) {
				this.billPieceService.editBillSecondInfo(billSecondDto);
				message = "修改按次计费成功";
			}else if (billType.equals("3") || billType.equals("4")) {
				this.billPieceService.editBillDistanceInfo(billDistanceDto);
				message = "修改里程计费成功";
			}else {
				return ViewDataUtil.buildFailureJson(1, "信息有误，修改失败");
			}
			return ViewDataUtil.buildSuccessJson(message);
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 服务类型，车辆类型权重信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/bill/getBillServeVehicleInfo.htm", method=RequestMethod.GET, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getBillServeVehicleInfo(HttpServletRequest request) {
		try {
			//获取服务类型权重信息
			BillServeDto billServeDto = this.billPieceService.findBillServeInfo();
			//获取按次计费信息
			BillVehicleDto billVehicleDto = this.billPieceService.findBillVehicleInfo();
			Map<String, Object> map = new HashMap<>();
			map.put("billServe", billServeDto);
			map.put("billVehicle", billVehicleDto);
			return ViewDataUtil.buildSuccessJson("权重信息", map);
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	
	/**
	 * 修改服务类型，车辆类型权重信息
	 * @param request
	 * @param billServeDto 服务类型权重信息
	 * @param BillVehicleDto 车辆类型权重信息
	 * @param billType 权重类型：1服务，2车辆
	 */
	@RequestMapping(value="/bill/modifyBillServeVehicleInfo.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	@SystemControllerLog(description="修改权重信息配置")
	public String modifyBillServeVehicleInfo(BillServeDto billServeDto, BillVehicleDto billVehicleDto, @RequestParam String billType) {
		try {
			String message = "";
			if (billType.equals("1")) {
				this.billPieceService.editBillServeInfo(billServeDto);
				message = "修改服务类型权重成功";
			}else if (billType.equals("2")) {
				this.billPieceService.editBillVehicleInfo(billVehicleDto);
				message = "修改车辆类型权重成功";
			}else {
				return ViewDataUtil.buildFailureJson(1, "信息有误，修改失败");
			}
			return ViewDataUtil.buildSuccessJson(message);
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 查询昼夜权重信息
	 * @param request
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/bill/getBillTimingInfo.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getBillTimingInfo(HttpServletRequest request, Page page) {
		List<BillTimingDto> billTimingDtos = this.billPieceService.findBillTimingInfo(page);
		return ViewDataUtil.buildSuccessPageJson("查询时间设置信息", billTimingDtos, billTimingDtos.size());
	}
	
	/**
	 * 回显昼夜权重信息
	 * @param timingType
	 * @return
	 */
	@RequestMapping(value="/bill/echoBillTimingInfo.htm", method=RequestMethod.GET, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String echoBillTimingInfo(@RequestParam Integer timingType) {
		try {
			BillTimingDto billTimingDto = this.billPieceService.findBillTimingInfoByType(timingType);
			return ViewDataUtil.buildSuccessJson("时间配置信息", billTimingDto);
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 修改昼夜权重配置
	 * @param billTimingDto
	 * @return
	 */
	@RequestMapping(value="/bill/modifyBillTimingInfo.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	@SystemControllerLog(description="修改时间配置")
	public String modifyBillTimingInfo(BillTimingDto billTimingDto) {
		try {
			this.billPieceService.editBillTimingInfo(billTimingDto);
			return ViewDataUtil.buildSuccessJson("修改成功");
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 查询车辆类型及车辆权重信息
	 * @param request
	 * @param page 分页信息
	 * @return
	 */
	@RequestMapping(value="/bill/getCarTypeInfo.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getCarTypeInfo(HttpServletRequest request, Page page) {
		List<BillCarTypeDto> billCarTypeDtos = this.billPieceService.findCarTypeInfo(page);
		return ViewDataUtil.buildSuccessPageJson("车辆类型及车辆权重", billCarTypeDtos, billCarTypeDtos.size());
	}
	
	/**
	 * 回显车辆类型及车辆权重信息
	 * @param carTypeId
	 * @return
	 */
	@RequestMapping(value="/bill/echoCarTypeInfo.htm", method=RequestMethod.GET, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String echoCarTypeInfo(@RequestParam Integer carTypeId) {
		try {
			BillCarTypeDto billCarTypeDto = this.billPieceService.findCarTypeInfoById(carTypeId);
			return ViewDataUtil.buildSuccessJson("车辆类型及车辆权重信息", billCarTypeDto);
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 修改车辆类型及车辆权重配置
	 * @param billTimingDto 车辆类型及车型权重信息
	 * @return
	 */
	@RequestMapping(value="/bill/modifyCarTypeInfo.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	@SystemControllerLog(description="修改车辆类型及车辆权重配置")
	public String modifyCarTypeInfo(BillCarTypeDto billCarTypeDto) {
		try {
			this.billPieceService.editCarTypeInfo(billCarTypeDto);
			return ViewDataUtil.buildSuccessJson("修改成功");
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 禁用或启用此车辆类型及权重
	 * @param billTimingDto 车辆类型及车型权重信息
	 * @return
	 */
	@RequestMapping(value="/bill/modifyCarTypeInfoIsDel.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	@SystemControllerLog(description="修改车辆类型及车辆权重配置")
	public String modifyCarTypeInfoIsDel(BillCarTypeDto billCarTypeDto) {
		try {
			this.billPieceService.editCarTypeInfoIsDel(billCarTypeDto);
			return ViewDataUtil.buildSuccessJson("修改成功");
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
}
